package com.dopplr.examples.modestmaps
{
    import com.dopplr.api.API;
    import com.dopplr.api.Traveller;
    import com.modestmaps.Map;
    import com.modestmaps.extras.MapControls;
    import com.modestmaps.geo.Location;
    import com.modestmaps.mapproviders.microsoft.MicrosoftHybridMapProvider;
    
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.ErrorEvent;
    import flash.events.Event;
    import flash.events.StatusEvent;
    import flash.text.TextField;

    [SWF(backgroundColor="#ffffff")]
    public class DopplrFellowsMap extends Sprite
    {
        public var api:API;
        public var map:Map;
        
        public function DopplrFellowsMap()
        {
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;

            var token:String = LoaderInfo(root.loaderInfo).parameters.token;
            if (token) {    
                // initialize API with token passed in from javascript
                api = new API(token);
                api.addEventListener(ErrorEvent.ERROR, onAPIError);
                api.addEventListener(StatusEvent.STATUS, onAPIStatus);
    
                // try loading the user's fellow travellers
                api.loadFellowTravellers(gotEm);

                map = new Map(stage.stageWidth, stage.stageHeight, true, new MicrosoftHybridMapProvider());
                map.setCenterZoom(new Location(0,0), 2);
                addChild(map);
                map.addChild(new MapControls(map));
                
                stage.addEventListener(Event.RESIZE, function(event:Event):void { map.setSize(stage.stageWidth, stage.stageHeight); });
            }
            else {
                var text:TextField = new TextField();
                text.text = "problem initializing dopplr access: no token found";
                text.width = text.textWidth + 4;
                addChild(text);
            }
        }
        
        public function gotEm(users:Array):void
        {
            // fellow travellers don't come with home or current cities, so...
            for each (var user:Traveller in users) {
                api.loadTravellerInfo(user.nick, gotHome);
            }
            
            // could also get them all at once, but it's a long call
            // api.loadTravellerInfo(users, gotHomes);
        }
        
        public function gotHome(user:Traveller):void
        {
            var marker:DopplrUserMarker = new DopplrUserMarker(user);
            map.putMarker(marker.name, marker.location, marker);
        }
        
        public function onAPIError(event:ErrorEvent):void
        {
            // do something about errors here
            var errorText:String = event.text;
            trace(errorText);
        }

        public function onAPIStatus(event:StatusEvent):void
        {
            // do something about status here
            trace(event.level + ": " + event.code);
        }
        
    }
}

import flash.display.Sprite;
import com.dopplr.api.Traveller;
import com.modestmaps.geo.Location;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.LoaderInfo;   

class DopplrUserMarker extends Sprite
{
    public var user:Traveller;
    public var location:Location;
    
    public function DopplrUserMarker(user:Traveller)
    {
        this.user = user;
        this.location = new Location(user.currentCity.latitude, user.currentCity.longitude);
        this.name = user.nick;
        
        var loader:Loader = new Loader();
        loader.load(new URLRequest(user.iconURL));
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onIconLoaded);
        addChild(loader);
        
        graphics.beginFill(user.currentCity.rgb);
        graphics.drawRect(-16-3, -16-3, 32+6, 32+6);
    }
    
    private function onIconLoaded(event:Event):void
    {
        var loader:Loader = (event.target as LoaderInfo).loader;
        loader.x = -loader.width/2;
        loader.y = -loader.height/2;
    }
}
